﻿using System.Collections.Generic;
using Brain.Rendering;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Brain.Rendering
{
    public class PostProcessorManager
    {
        public List<IBasePostProcessor> PostProcessors { get; protected set; }

        private int count = 0;

        public SpriteBatch SpriteBatch;

        public PostProcessorManager(ushort postProcessors)
        {
            SpriteBatch = new SpriteBatch(Engine.Instance.GraphicsDevice);
            PostProcessors = new List<IBasePostProcessor>();
        }

        public void AddPostProcessor(IBasePostProcessor pp)
        {
            PostProcessors.Add(pp);
        }


        public virtual void Draw(Texture2D Scene)
        {
            IBasePostProcessor last = null;
            IBasePostProcessor next = null;
            Texture2D newScene = null;

            for (int i = 0; i < PostProcessors.Count; i++)
            {
                if (last == null)
                {
                    last = PostProcessors[i];
                    last.InputTexture = Scene;
                    last.RequestOutputTexture = false;
                    if (i + 1 < PostProcessors.Count)
                    {
                        next = PostProcessors[i + 1];
                        last.RequestOutputTexture = true;
                        if (last.OutputTexture == null || last.OutputTexture.IsDisposed)
                        {
                            last.OutputTexture = MakeRenderTarget();
                        }
                    }

                    last.Draw();
                    newScene = last.OutputTexture;
                }
                else
                {
                    if (next != null)
                    {
                        last = next;
                        last.RequestOutputTexture = false;
                        last.InputTexture = newScene;
                        if (i + 1 < PostProcessors.Count)
                        {
                            next = PostProcessors[i + 1];
                            last.RequestOutputTexture = true;
                            if (last.OutputTexture == null || last.OutputTexture.IsDisposed)
                            {
                                last.OutputTexture = MakeRenderTarget();
                            }
                        }

                        last.Draw();
                        newScene = last.OutputTexture;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (PostProcessors.Count == 0)
            {
                SpriteBatch.Draw(Scene, Vector2.Zero, Color.White);
            }
        }

        public virtual RenderTarget2D MakeRenderTarget()
        {
            PresentationParameters pp = Engine.Instance.GraphicsDevice.PresentationParameters;
            return new RenderTarget2D(Engine.Instance.GraphicsDevice,
                            pp.BackBufferWidth,
                            pp.BackBufferHeight);
        }
    }
}
